//
//  YWPhotoBrowserAnimator.m
//  videoTest3
//
//  Created by jun peng on 2022/6/10.
//

#import "YWPhotoBrowserAnimator.h"

@interface YWPhotoBrowserAnimator ()<UIViewControllerTransitioningDelegate,UIViewControllerAnimatedTransitioning>

@property(nonatomic,assign)BOOL isPresented;
@end

@implementation YWPhotoBrowserAnimator

//--------------UIViewControllerTransitioningDelegate
-(id<UIViewControllerAnimatedTransitioning>)animationControllerForPresentedController:(UIViewController *)presented presentingController:(UIViewController *)presenting sourceController:(UIViewController *)source{
    self.isPresented = YES;
    return self;
}

-(id<UIViewControllerAnimatedTransitioning>)animationControllerForDismissedController:(UIViewController *)dismissed{
    self.isPresented = NO;
    return self;
}


//--------------UIViewControllerAnimatedTransitioning
-(NSTimeInterval)transitionDuration:(id<UIViewControllerContextTransitioning>)transitionContext{
    return 0.3;
}


-(void)animateTransition:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    //self.isPresented判断是弹出还是消失
    self.isPresented ? [self animationForPresentedView:transitionContext] : [self animationForDismissView:transitionContext];
}

//--------------自制方法
///*了解一下一下属性：*/
//imageView：只负责做动画、动画完成后要移除
//presentedView：弹出的view
//dismissView：消失的view
//transitionContext.containerView：负责做动画的容器
-(void)animationForPresentedView:(id<UIViewControllerContextTransitioning>)transitionContext{
    // 0.nil值校验
   
    if (self.presentedDelegate == nil || self.indexPath == nil) {
        return;
    }
    
    // 1.取出弹出的View
    UIView *presentedView = [transitionContext viewForKey:UITransitionContextToViewKey];
    
    // 2.将prensentedView添加到containerView中
    [transitionContext.containerView addSubview:presentedView];

    // 3.获取执行动画的imageView
    CGRect startRect = CGRectZero;
    if ([self.presentedDelegate respondsToSelector:@selector(startRect:)]) {
        startRect = [self.presentedDelegate startRect:self.indexPath];
    }
    
    CGRect endRect = CGRectZero;
    if ([self.presentedDelegate respondsToSelector:@selector(endRect:)]) {
       endRect = [self.presentedDelegate endRect:self.indexPath];
    }
    
    //这是一个虚拟imageView主要是用来做动画的、完成动画后要删除的
    UIImageView *imageView = [[UIImageView alloc] init];
    if ([self.presentedDelegate respondsToSelector:@selector(imageView:)]) {
        imageView = [self.presentedDelegate imageView:self.indexPath];
    }
    
    [transitionContext.containerView addSubview:imageView];
    
    imageView.frame = startRect;

    // 4.执行动画
    presentedView.alpha = 0.0;
    transitionContext.containerView.backgroundColor = [UIColor blackColor];
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        
        imageView.frame = endRect;
        
    } completion:^(BOOL finished) {
        [imageView removeFromSuperview];
        presentedView.alpha = 1.0;
        transitionContext.containerView.backgroundColor = [UIColor clearColor];
        [transitionContext completeTransition:YES];
        
    }];
    
}

-(void)animationForDismissView:(id<UIViewControllerContextTransitioning>)transitionContext{
    
    // nil值校验
    if (self.dismissDelegate == nil || self.presentedDelegate == nil) {
        return;
    }
  
    // 1.取出消失的View
    UIView *dismissView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    [dismissView removeFromSuperview];
    
    // 2.获取执行动画的ImageView(依然是虚拟的、但后面不需要删除这个imageView、因为执行动画完整个transitionContext.containerView都被移除了)
    //这个imageView已经包含了dismissDelegate传过来的frame
    UIImageView *imageView = [[UIImageView alloc] init];
    if ([self.dismissDelegate respondsToSelector:@selector(imageViewForDimissView)]) {
        imageView = [self.dismissDelegate imageViewForDimissView];
    }

    [transitionContext.containerView addSubview:imageView];
    
    NSIndexPath *dismissIndexPath = [[NSIndexPath alloc] init];
    if ([self.dismissDelegate respondsToSelector:@selector(indexPathForDismissView)]) {
        dismissIndexPath = [self.dismissDelegate indexPathForDismissView];
    }
    
  
    // 3.执行动画
    [UIView animateWithDuration:[self transitionDuration:transitionContext] animations:^{
        CGRect dismissToRect = CGRectZero;
        if ([self.presentedDelegate respondsToSelector:@selector(startRect:)]) {
            
            //这个代理方法既要imgEndIndexPath也要contentBeginIndexPath才能拿到图片动画结束位置
            dismissToRect = [self.presentedDelegate startRect:dismissIndexPath];
        }
        imageView.frame = dismissToRect;
        
    }completion:^(BOOL finished) {
        [transitionContext completeTransition:YES];
    }];
   
    
}



@end
